//
//  WJPublishViewController.m
//  BeDeJie
//
//  Created by jerry on 2017/1/25.
//  Copyright © 2017年 jerry. All rights reserved.
//

#import "WJPublishViewController.h"
#import "WJPublishWordViewController.h"
#import "WJNavigationController.h"
#import "WJPublishButton.h"
#import <POP.h>
#import "WJLoginRegisterViewController.h"

static CGFloat const springFactor = 10;

@interface WJPublishViewController ()

//标题
@property (nonatomic, weak) UIImageView *sloganImageView;
//按钮
@property (nonatomic, strong) NSMutableArray *buttonArray;
//动画时间
@property (nonatomic, strong) NSArray *timeArray;

@end

@implementation WJPublishViewController

//懒加载按钮
- (NSMutableArray *)buttonArray {
    if (!_buttonArray) {
        _buttonArray = [NSMutableArray array];
    }
    return _buttonArray;
}

//懒加载动画时间
- (NSArray *)timeArray {
    if (!_timeArray) {
        //时间间隔
        CGFloat interval = 0.1;
        _timeArray = @[@(5 * interval),
                       @(4 * interval),
                       @(3 * interval),
                       @(2 * interval),
                       @(0 * interval),
                       @(1 * interval),
                       @(6 * interval)];  //标语动画时间
    }
    return _timeArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //禁止交互
    self.view.userInteractionEnabled = NO;
    
    //按钮
    [self setupButtonArray];
    
    //标语
    [self setupSloganView];
    
}

- (IBAction) cancel {
    [self exit:nil];
}

-(void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self exit:nil];
}

- (void) setupButtonArray {
    //数据
    NSArray *imageArray = @[@"publish_video",@"publish_picture",@"publish_text",@"publish_audio",@"publish_review",@"publish_offline"];
    NSArray *titleArray = @[@"发视频",@"发图片",@"发段子",@"发声音",@"审贴",@"离线下载"];
    
    //图片总数
    NSUInteger count = imageArray.count;
    //每行最大的列数
    int maxColCount = 3;
    //行数
    NSUInteger rowCount = (count + maxColCount - 1) / maxColCount;
    
    //按钮的宽度
    CGFloat buttonWidth = screenWidth / maxColCount;
    //按钮高度
    CGFloat buttonHeight = buttonWidth * 0.85;
    //按钮的Y值
    CGFloat buttonStartY = (screenHeight - rowCount * buttonHeight) * 0.5;
    for (int i = 0; i < count; i++) {
        //创建并添加按钮
        WJPublishButton *publishButton = [WJPublishButton buttonWithType:UIButtonTypeCustom];
        publishButton.wj_width = -1;  //按钮的尺寸设为0,还是能看到一个圆点。设为负数，则看不见
        [publishButton addTarget:self action:@selector(publishButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.buttonArray addObject:publishButton];
        [self.view addSubview:publishButton];
        
        //内容
        [publishButton setImage:[UIImage imageNamed:imageArray[i]] forState:UIControlStateNormal];
        [publishButton setTitle:titleArray[i] forState:UIControlStateNormal];
        
        //frame
        CGFloat buttonX = (i % maxColCount) * buttonWidth;
        CGFloat buttonY = buttonStartY + (i / maxColCount) * buttonHeight;
        
        //动画
        //kPOPViewFrame 是框架中的常量
        POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
        anim.fromValue = [NSValue valueWithCGRect:CGRectMake(buttonX, buttonY - screenHeight, buttonWidth, buttonHeight)];
        anim.toValue = [NSValue valueWithCGRect:CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight)];
        anim.springSpeed = springFactor;
        anim.springBounciness = springFactor;
        anim.beginTime = CACurrentMediaTime() + [self.timeArray[i] doubleValue];  //CACurrentMediaTime()获取当前时间
        [publishButton pop_addAnimation:anim forKey:nil];
    }
    
}

- (void) setupSloganView {
    
    CGFloat sloganY = screenHeight * 0.2;
    // 添加
    UIImage *sloganImage = [UIImage imageNamed:@"publish_slogan"];
    UIImageView *sloganImageView = [[UIImageView alloc] initWithImage:sloganImage];
    sloganImageView.wj_y = sloganY - screenHeight;
    sloganImageView.wj_centerX = screenWidth * 0.5;
    [self.view addSubview:sloganImageView];
    self.sloganImageView = sloganImageView;
    
    //动画
    myWeakSelf;
    POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionY];
    anim.toValue = @(sloganY);
    anim.springSpeed = springFactor;
    anim.springBounciness = springFactor;
    anim.beginTime = CACurrentMediaTime() + [self.timeArray.lastObject doubleValue];  //CACurrentMediaTime()获取当前时间
    [anim setCompletionBlock:^(POPAnimation *anim, BOOL finished){
        weakSelf.view.userInteractionEnabled = YES;   //开始交互
    }];
    [sloganImageView.layer pop_addAnimation:anim forKey:nil];
}

#pragma mark 退出动画
- (void) exit:(void (^)()) task {
    //禁止交互
    self.view.userInteractionEnabled = NO;
    
    //按钮执行动画
    for (int i = 0; i < self.buttonArray.count; i++) {
        WJPublishButton *publishButton = self.buttonArray[i];
        
        POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionY];
        anim.toValue = @(publishButton.layer.position.y + screenHeight);
        anim.beginTime = CACurrentMediaTime() + [self.timeArray[i] doubleValue];  //CACurrentMediaTime()获取当前时间
        [publishButton.layer pop_addAnimation:anim forKey:nil];
    }
    
    //标题执行动画
    myWeakSelf;
    POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionY];
    anim.toValue = @(self.sloganImageView.layer.position.y + screenHeight);
    anim.beginTime = CACurrentMediaTime() + [self.timeArray.lastObject doubleValue];  //CACurrentMediaTime()获取当前时间
    [anim setCompletionBlock:^(POPAnimation *anim, BOOL finished){
        [weakSelf dismissViewControllerAnimated:NO completion:^{
            //动画执行完，执行其他动作
            //写法一：
//            if (task) {
//                task();
//            }
            //写法二：
            !task ? :task();
        }];
    }];
    [self.sloganImageView.layer pop_addAnimation:anim forKey:nil];
}

#pragma mark 点击
- (void) publishButtonClick: (WJPublishButton *) button {
    
    WJPublishWordViewController *publishWordController = [[WJPublishWordViewController alloc] init];
    WJNavigationController *wordNavController = [[WJNavigationController alloc] initWithRootViewController:publishWordController];
    [self presentViewController:wordNavController animated:YES completion:^{
        WJLog(@"打开WJPublishWordViewController");
    }];
    
//    [self exit:^{
//        //按钮索引，执行动作
//        NSUInteger index = [self.buttonArray indexOfObject:button];
//        switch (index) {
//            case 0:
//                WJLog(@"发视频");
//                break;
//             
//            case 1:
//                WJLog(@"发图片");
//                break;
//            case 2: {
//                WJPublishWordViewController *publishWordController = [[WJPublishWordViewController alloc] init];
//                WJNavigationController *wordNavController = [[WJNavigationController alloc] initWithRootViewController:publishWordController];
//                [self.view.window.rootViewController presentViewController:wordNavController animated:YES completion:^{
//                    WJLog(@"打开WJPublishWordViewController");
//                }];
//                break;
//            }
//            case 3:
//                
//                break;
//            case 4:
//                
//                break;
//            default:
//                WJLog(@"其它");
//                break;
//        }
//    }];
}



@end
